<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="客户姓名" prop="customerN
      ame">
        <el-input v-model="formData.customerName" placeholder="请输入客户姓名" />
      </el-form-item>
      <el-form-item label="客户年龄" prop="customerAge">
        <el-input v-model="formData.customerAge" placeholder="请输入客户年龄" />
      </el-form-item>
      <el-form-item label="客户性别" prop="customerSex">
        <el-radio-group v-model="formData.customerSex">
          <el-radio label="1" value="男">男</el-radio>
          <el-radio label="2" value="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="身份证号码" prop="idcard">
        <el-input v-model="formData.idcard" placeholder="请输入身份证号码" />
      </el-form-item>
      <el-form-item label="房间号" prop="roomNumber">
        <el-input v-model="formData.roomNumber" placeholder="请输入房间号" />
      </el-form-item>
      <el-form-item label="建筑编号（未使用）" prop="buildingId">
        <el-input v-model="formData.buildingId" placeholder="请输入建筑编号（未使用）" />
      </el-form-item>
      <el-form-item label="档案编号" prop="recordId">
        <el-input v-model="formData.recordId" placeholder="请输入档案编号" />
      </el-form-item>
      <el-form-item label="老人类型： 活力老人 自理老人 护理老人" prop="elderType">
        <el-select v-model="formData.elderType" placeholder="请选择老人类型： 活力老人 自理老人 护理老人">
          <el-option label="活力老人" value="活力老人" />
          <el-option label="自理老人" value="自理老人" />
          <el-option label="护理老人" value="护理老人" />
        </el-select>
      </el-form-item>
      <el-form-item label="入住时间" prop="checkinDate">
        <el-date-picker
          v-model="formData.checkinDate"
          type="date"
          value-format="x"
          placeholder="选择入住时间"
        />
      </el-form-item>
      <el-form-item label="合同到期时间" prop="expirationDate">
        <el-date-picker
          v-model="formData.expirationDate"
          type="date"
          value-format="x"
          placeholder="选择合同到期时间"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactTel">
        <el-input v-model="formData.contactTel" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="床位id" prop="bedId">
        <el-input v-model="formData.bedId" placeholder="请输入床位id" />
      </el-form-item>
      <el-form-item label="精神状况：正常 非正常" prop="psychosomaticState">
        <el-input v-model="formData.psychosomaticState" placeholder="请输入精神状况：正常 非正常" />
      </el-form-item>
      <el-form-item label="注意事项" prop="attention">
        <el-input v-model="formData.attention" placeholder="请输入注意事项" />
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
          v-model="formData.checkinDate"
          type="date"
          value-format="x"
          placeholder="选择入住时间"
        />
      </el-form-item>
      <el-form-item label="身高" prop="height">
        <el-input v-model="formData.height" placeholder="请输入身高" />
      </el-form-item>
      <el-form-item label="婚姻状况" prop="maritalStatus">
        <el-radio-group v-model="formData.maritalStatus">
          <el-radio label="1" value="已婚">已婚</el-radio>
          <el-radio label="1" value="未婚">未婚</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="体重" prop="weight">
        <el-input v-model="formData.weight" placeholder="请输入体重" />
      </el-form-item>
      <el-form-item label="血型" prop="bloodType">
        <el-select v-model="formData.bloodType" placeholder="请选择血型">
          <el-option label="A" value="A" />
          <el-option label="B" value="B" />
          <el-option label="AB" value="AB" />
          <el-option label="O" value="O" />
        </el-select>
      </el-form-item>
      <el-form-item label="照片地址" prop="filepath">
        <el-input v-model="formData.filepath" placeholder="请输入照片地址" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CustomerApi, CustomerVO } from '@/api/yyzx/customer'

/** 客户信息 表单 */
defineOptions({ name: 'CustomerForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  customerName: undefined,
  customerAge: undefined,
  customerSex: undefined,
  idcard: undefined,
  roomNumber: undefined,
  buildingId: undefined,
  recordId: undefined,
  elderType: undefined,
  checkinDate: undefined,
  expirationDate: undefined,
  contactTel: undefined,
  bedId: undefined,
  psychosomaticState: undefined,
  attention: undefined,
  birthday: undefined,
  height: undefined,
  maritalStatus: undefined,
  weight: undefined,
  bloodType: undefined,
  filepath: undefined,
})
const formRules = reactive({
  customerName: [{ required: true, message: '客户姓名不能为空', trigger: 'blur' }],
  customerAge: [{ required: true, message: '客户年龄不能为空', trigger: 'blur' }],
  customerSex: [{ required: true, message: '客户性别不能为空', trigger: 'blur' }],
  idcard: [{ required: true, message: '身份证号码不能为空', trigger: 'blur' }],
  roomNumber: [{ required: true, message: '房间号不能为空', trigger: 'blur' }],
  elderType: [{ required: true, message: '老人类型： 活力老人 自理老人 护理老人不能为空', trigger: 'change' }],
  contactTel: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
  bedId: [{ required: true, message: '床位id不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CustomerApi.getCustomer(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CustomerVO
    if (formType.value === 'create') {
      await CustomerApi.createCustomer(data)
      message.success(t('common.createSuccess'))
    } else {
      await CustomerApi.updateCustomer(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    customerName: undefined,
    customerAge: undefined,
    customerSex: undefined,
    idcard: undefined,
    roomNumber: undefined,
    buildingId: undefined,
    recordId: undefined,
    elderType: undefined,
    checkinDate: undefined,
    expirationDate: undefined,
    contactTel: undefined,
    bedId: undefined,
    psychosomaticState: undefined,
    attention: undefined,
    birthday: undefined,
    height: undefined,
    maritalStatus: undefined,
    weight: undefined,
    bloodType: undefined,
    filepath: undefined,
  }
  formRef.value?.resetFields()
}
</script>
